package com.springboot.frame.excel.handler;

import com.springboot.frame.excel.util.DecimalFormatKit;
import org.apache.poi.ss.usermodel.Cell;

/**
 * @author madman
 */
public class PrimitiveCellHandler extends AbstractBaseCellHandler<Number> {

  public static final PrimitiveCellHandler INSTANCE = new PrimitiveCellHandler();

  @Override
  public void doWrite(Cell cell, Number data) {
    if (data != null) {
      cell.setCellValue(data.longValue());
    }
  }

  @Override
  public Number read(Cell cell, Object object) {
    switch (cell.getCellTypeEnum()) {
      case NUMERIC:
        return Integer.valueOf(DecimalFormatKit.format(cell.getNumericCellValue()));
      case STRING:
        return Integer.valueOf(DecimalFormatKit.format(Double.valueOf(cell.getStringCellValue())));
      case BOOLEAN:
        return cell.getBooleanCellValue() ? 1 : 0;
      default:
        return null;
    }
  }

}
